package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.activity.MessageCenterActivity;
import com.qire.manhua.databinding.ActivityMessageCenterBinding;
import com.qire.manhua.databinding.LayoutMessageCenterItemBinding;
import com.qire.manhua.model.viewModel.impl.MessageCenterViewModel;
import com.qire.manhua.model.viewModel.impl.MessageCenterViewModel.PlatformMessageItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;

/**
 * 消息中心控制器
 */
public class MessageCenterController extends EventController<MessageCenterActivity> implements AutoVariable<LayoutMessageCenterItemBinding> {

    private MessageCenterViewModel viewModel;

    public MessageCenterController(@NonNull MessageCenterActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityMessageCenterBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setMessageCenterController(this);
    }

    @Override
    public void bindVariable(LayoutMessageCenterItemBinding viewBind) {
        viewBind.setMessageCenterController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(MessageCenterViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.loadMoreData();
    }

    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadData();
    }

    public void backOnClick(View view) {
        owner.onBackPressed();
    }

    public void allReadOnClick(View view) {
        viewModel.allMarkRead();
    }

    public void enablePushOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.MessageSetupPage).navigation();
    }

    public void closeEnablePushTipsOnClick(View view) {
        viewModel.setEnablePushTipsVisible(true);
    }

    public void commentItemOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.MessageCommentListPage).navigation();
    }

    public void likeItemOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.MessageLikeListPage).navigation();
    }

    public void platformMessageItemOnClick(View view) {
        PlatformMessageItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), PlatformMessageItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.readMessage();
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                    .withData("url", itemViewModel.messageLink)
                    .withData("title", itemViewModel.messageTitle)
                    .navigation();
        }
    }

}
